package segimasat.partition;


import segimasat.gui.PartitionInfo;
import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.Date;
import segimasat.utils.Utils;


public class Listener implements WindowListener, MouseListener, MouseMotionListener{
    static long ELAPSED = 50;

    Partition p;
    PartitionInfo frame;
    long time;

    public Listener(Partition p, PartitionInfo frame){
        this.p = p;
        this.frame = frame;
    }

    public void mouseClicked(MouseEvent e){
        if (frame.highlightCheckBox.isSelected()){
            frame.highlightFixed = !frame.highlightFixed;
            if (! frame.highlightFixed){
                int x = p.out.imc.offScreenX(e.getX());
                int y = p.out.imc.offScreenY(e.getY());
                int region = p.lbl[p.out.impr.getWidth() * y + x];
                p.colourComponent((Component)p.components.get(frame.highlighted)/*, p.components.get(frame.highlighted).color*/);
                p.colourComponent((Component)p.components.get(region), p.out.newPixelValue(Color.YELLOW.getRGB()));
                frame.highlighted = region;
            }
        }
    }

    public void mouseMoved(MouseEvent e){
        int x = p.out.imc.offScreenX(e.getX());
        int y = p.out.imc.offScreenY(e.getY());
        int pos = p.out.impr.getWidth() * y + x;
        int region = p.lbl[pos];
        Component current = (Component)p.components.get(region);
        if (region != frame.highlighted && frame.highlightCheckBox.isSelected() && !frame.highlightFixed){
            p.colourComponent((Component)p.components.get(frame.highlighted)/*, p.components.get(frame.highlighted).color*/);
            p.colourComponent(current, p.out.newPixelValue(Color.YELLOW.getRGB()));
            frame.highlighted = region;
        }
        if ((new Date()).getTime() - time > ELAPSED){
            frame.text_xy.setText(Utils.xy(x, y));
            frame.text_rgb.setText(/*current.color.getString(0)*/ current.getColorString());
            frame.text_rgb1.setText(/*current.color.getString(0)*/ current.getColorString());
            frame.text_rgbOriginal.setText(/*p.f.getPixel(pos).getString(0)*/ p.getColorString(pos));
            frame.text_label.setText("" + region);
            frame.text_componentSize.setText("" + current.pixels.size());
            frame.text_componentLocal.setText(/*"" + current.localRange*/ current.getLocalRange());
            frame.text_componentGlobal.setText(/*"" + current.max.rangeVector(current.min).getString(0)*/ current.getGlobalRange());
            frame.text_min.setText(/*current.min.getString(0)*/ current.getMin());
            frame.text_max.setText(/*current.max.getString(0)*/ current.getMax());
            time = (new Date()).getTime();
        }
    }

    public void windowClosed(WindowEvent e){
        if (e.getWindow() == p.out.win){
            frame.dispose();
            //p.dispose();
        }
    }

    public void windowOpened(WindowEvent e){}
    public void windowClosing(WindowEvent e){}
    public void windowIconified(WindowEvent e){}
    public void windowDeiconified(WindowEvent e){}
    public void windowActivated(WindowEvent e){}
    public void windowDeactivated(WindowEvent e){}

    public void mousePressed(MouseEvent e) {}
    public void mouseReleased(MouseEvent e) {}
    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
    public void mouseDragged(MouseEvent e) {}
}